//
//  PopupController.swift
//  CommonUI
//
//  Created by sunlubo on 16/7/15.
//  Copyright © 2016年 milanosoft. All rights reserved.
//

import UIKit
import SnapKit

public typealias PopupCallback = (Any?) -> Void

/// 弹窗 - 基类，负责基础功能支持
open class PopupController: UIViewController {
    @IBOutlet private weak var view1: UIView!
    @IBOutlet private weak var view2: UIView!
    @IBOutlet weak var leftButton: UIButton!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var rightButton: UIButton!
    @IBOutlet weak var contentView: UIView!

    /// 动画持续时长
    public var animationDuration = 0.25
    /// 内容视图的高度
    public var contentHeight = 400
    /// 用于配置标题栏UI的函数
    public var titleBarSetupHandler: ((UIButton, UILabel, UIButton) -> Void)?
    /// 设置内容视图
    public var contentSetupHandler: ((UIView) -> Void)?
    /// 用于操作完成后回调的函数
    public var completionHandler: PopupCallback?
    /// 操作结果
    public var result: Any?

    public convenience init() {
        self.init(nibName: "PopupController", bundle: nil)
    }

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    public required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    open override func viewDidLoad() {
        super.viewDidLoad()

        setupContentView()
        titleLabel.text = self.title
        view1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hide)))
        view2.snp.makeConstraints({ $0.height.equalTo(contentHeight) })
    }

    /// 配置视图内容，如果子类重写了该方法，必须调用父类的实现
    open func setupContentView() {
        titleBarSetupHandler?(leftButton, titleLabel, rightButton)
        contentSetupHandler?(contentView)
    }

    /// 显示弹窗
    ///
    /// - Parameter completionHandler: 回调函数
    public func showInWindow(completionHandler: @escaping PopupCallback) {
        self.completionHandler = completionHandler

        self.view.frame = currentWindow.bounds
        currentWindow.addSubview(self.view)

        view1.alpha = 0.0
        view2.transform = CGAffineTransform(translationX: 0, y: view2.frame.height)
        UIView.animate(withDuration: animationDuration) {
            self.view1.alpha = 0.3
            self.view2.transform = CGAffineTransform.identity
        }
    }

    /// 关闭弹窗
    public func hide() {
        UIView.animate(withDuration: animationDuration,
                       animations: { _ in
                           self.view1.alpha = 0.0
                           self.view2.transform = CGAffineTransform(translationX: 0, y: self.view1.frame.height)
                       },
                       completion: { _ in
                           self.view.removeFromSuperview()
                           self.completionHandler?(self.result)
        })
    }

    @IBAction
    private func leftButtonTapped() {
        handleEventForLeftButton()
        hide()
    }

    @IBAction
    private func rightButtonTapped() {
        handleEventForRightButton()
        hide()
    }

    /// 左侧按钮点击事件处理
    open func handleEventForLeftButton() {
        // subclass override
    }

    /// 右侧按钮点击事件处理
    open func handleEventForRightButton() {
        // subclass override
    }
}
